#include "fun.h"

u8 bdata com;
sbit BUZZER = com^6;
sbit RELAY = com^4;
bit ui_status = 1;
bit out_mode;
bit led_flag = 1;
bit smg_flag = 1;
u8 mode_update = 1;

void buzzer(u8 cmd)
{
    BUZZER = cmd;
    P0 = com;
    P2 = 0xa0;
    _nop_();
    P2 = 0x00;
}

void relay(u8 cmd)
{
    RELAY = cmd;
    P0 = com;
    P2 = 0xa0;
    _nop_();
    P2 = 0x00;
}

void key_scan(void)
{
	u8 i;
    static u8 key_up = 1;
    if(key_up&&(P33==0||P32==0||P31==0||P30==0))
    {
        key_up = 0;
        if(!P33)
            ui_status = ~ui_status;
        else if(!P32)
		{
			out_mode = ~out_mode;
			mode_update = 1;
		}
        else if(!P31)
            led_flag = ~led_flag;
        else if(!P30)
        {
            smg_flag = ~smg_flag;
            if(!smg_flag)
            {
                for(i = 0;i < 8;i++)
                    smg_disp(0x00,0x01<<i);
            }
        }
    }
    else if(P33&&P32&&P31&&P30)
        key_up = 1;
}

void led(u8 ucode)
{
    P0 = ~ucode;
    P2 = 0x80;
    _nop_();
    P2 = 0x00;
}

void smg_disp(u8 num,u8 position)
{
    P0 = 0xff;
    P2 = 0xe0;
    _nop_();
    P2 = 0x00;

    P0 = position;
    P2 = 0xc0;
    _nop_();
    P2 = 0x00;

    P0 = ~num;
    P2 = 0xe0;
    _nop_();
    P2 = 0x00;
}
